package dli_and_obs.job_utils

import java.io.File
import java.util.Properties
import dli_and_obs.utils
/**
  * @Description 用于包传并跑任务的控制工具
  * @Author liutaowei
  * @Date 2019-5-22 15:16
  */
object dli_jar_loadAndRun {

  def jar_load_and_run(properties:Properties):String={

    val obj_url = jar_load_obs(properties)  // 获取到jar包obs的http路径

    val resources = utils.dli_utils.jar_load_dli(obj_url,properties) // 上传jar包到dli并 获取到dli的集群资源
    val job_state = utils.dli_utils.batchJobs(resources,properties) // 任务提交

    return "success"
  }

  /**
    *@Author: Liutaowei
    *@Date:2019-5-20 10:11
    *@Description: jar包上传obs
    */
  private def jar_load_obs(properties:Properties):String={
    val jar_local_path =properties.getProperty("jar_local_path")
    val object_path =properties.getProperty("object_path")
    val bucketname =properties.getProperty("bucketname")

    val jar_path = jar_check(jar_local_path)
    val obs_path = object_path.replace("\\","/").replaceAll("[/]$","") + "/" + jar_path.getName
    val obsClient = utils.obs_utils.get_obs_client(properties)

    // 具体
    if("" == jar_path) {println("传入路径存在问题,请仔细检查")
      return ""
    } else {
      utils.obs_utils.file_load_obs(jar_path, obsClient, properties)
      val obj_url = "https://" + bucketname +".obs.cn-north-1.myhuaweicloud.com/" + obs_path
      obsClient.close()
      return obj_url
    }
  }


  /**
  *@Author: Liutaowei
  *@Date:2019-5-20 10:10
  *@Description: 检查路径是否为jar包或文件夹下是否有jar包
  */
  private def jar_check(jar_local_path:String)={
    val path_1 = new File(jar_local_path)
    var path:File = null
    if(path_1.isFile && jar_local_path.endsWith("jar")){
      path = path_1
    }else if(path_1.isDirectory){
      val f_list = path_1.list().filter(_.endsWith("jar"))
      if(null != f_list) path = new File(path_1 +"/"+ f_list.take(1).mkString(""))
    }
    path
  }
}
